﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using ShevaEngine.Core.Interfaces;
using ShevaEngine.Core.Modules.Input;

namespace ShevaEngine2GUI
{
    /// <summary>
    /// Gui manager.
    /// </summary>
    public class GuiManager : IUpdateAble, IDisposable, IGui
    {
        /// <summary>Cursor.</summary>
        private MouseCursor _Cursor;
        /// <summary>Cursor.</summary>
        public MouseCursor Cursor
        {
            get { return this._Cursor; }
            set
            {
                if (this._Cursor != null)
                    this._Cursor.CursorMoved -= new OnMouseCursorMove(CursorMoved);

                this._Cursor = value;

                if (this._Cursor != null)
                    this._Cursor.CursorMoved += new OnMouseCursorMove(CursorMoved);
            }
        }
       
        /// <summary>Mouse layer.</summary>
        private Layer _MouseLayer;
        /// <summary>Mouse layer.</summary>
        public Layer MouseLayer
        {
            get { return this._MouseLayer; }
            set
            {
                if (this._MouseLayer != value)
                {
                    if (this._MouseLayer != null)
                        this._MouseLayer.Cursor = null;

                    if (value != null)
                        value.Cursor = this.Cursor;

                    this._MouseLayer = value;
                }
            }
        }
        /// <summary>Size.</summary>
        private Vector2 _Size;
        /// <summary>Size.</summary>
        public Vector2 Size
        {
            get { return this._Size; }
            set
            {
                if (this._Size != value)
                {
                    this._Size = value;

                    foreach (Layer layer in this.Layers)
                        layer.Redesign();
                }
            }
        }
        /// <summary>Layers.</summary>
        public List<Layer> Layers;

        /// <summary>
        /// Constructor.
        /// </summary>
        public GuiManager(Vector2 size)
        {
            this.Layers = new List<Layer>();
            this.Cursor = MouseCursor.Default;

            this.Size = size;

            InputManager.Instance.Mouse.OnMouseMove += new OnMouseMoveDelegate(Mouse_OnMouseMove);
        }

        /// <summary>
        /// On mouse move.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="position"></param>
        private void Mouse_OnMouseMove(object sender, Vector2 position)
        {
            this.Cursor.SetCursorPosition(position);
        }

        /// <summary>
        /// Dispose.
        /// </summary>
        public void Dispose()
        {

        }

        /// <summary>
        /// Cursor moved method.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="position"></param>
        private void CursorMoved(object sender, Vector2 position)
        {
            foreach (Layer layer in this.Layers)
                layer.MouseMove(position);
        }

        /// <summary>
        /// Method Update.
        /// </summary>
        /// <param name="time"></param>
        public void Update(GameTime time)
        {
            foreach (Layer layer in this.Layers)
                layer.Update(time);    
        }

        /// <summary>
        /// Method draw.
        /// </summary>
        public void Draw()
        {
            foreach (Layer layer in this.Layers)
                layer.Draw();    
        }

        /// <summary>
        /// Method creates new layer.
        /// </summary>
        /// <returns></returns>
        public Layer CreateNewLayer()
        {
            return new Layer(this);
        }
    }
}
